package com.example.keymanagertwo;

import android.content.DialogInterface;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import com.example.keymanagertwo.Adapter.keyadapter;
import com.example.keymanagertwo.Bean.Bean;
import com.example.keymanagertwo.KeyDao.KeyDao;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {
    private ListView lv;
    private Handler handler = new Handler();
    private List<Bean> list;
    private AlertDialog builder;
    private List<Bean> getall;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = findViewById(R.id.lv);
        KeyDao key = new KeyDao(this);
        getall = key.getall();
        keyadapter keyadapter = new keyadapter(getall);
        lv.setAdapter(keyadapter);
        lv.setOnItemClickListener(this);


    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        if (adapterView == lv) {
            if(builder==null){
            builder = new AlertDialog.Builder(this)
                    .setCancelable(false)
                    .setTitle("请稍后")
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            Toast.makeText(MainActivity.this, "你点击了取消", Toast.LENGTH_SHORT).show();
                            handler.removeCallbacks(r);
 }
                    })
                    .create();
            }


        builder.setMessage("你正在开门" + getall.get(i).getRomm());
        builder.show();
    }

        handler.postDelayed(r, 3000);


    }

    private Runnable r = new Runnable() {
        @Override
        public void run() {
           builder.dismiss();
            Toast.makeText(MainActivity.this, "开门成功", Toast.LENGTH_SHORT).show();


        }
    };

    @Override
    protected void onDestroy() {

        handler.removeCallbacksAndMessages(null);
        super.onDestroy();
    }
}
